package com.benio.ycfdisplayer.fresco;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.View;

import com.benio.ycfdisplayer.displayer.DisplayListener;
import com.benio.ycfdisplayer.displayer.OnStartListener;
import com.benio.ycfdisplayer.displayer.Options;
import com.benio.ycfdisplayer.displayer.Target;
import com.facebook.common.references.CloseableReference;
import com.facebook.datasource.BaseDataSubscriber;
import com.facebook.datasource.DataSource;
import com.facebook.imagepipeline.image.CloseableBitmap;
import com.facebook.imagepipeline.image.CloseableImage;

class BitmapDataSubscriber extends BaseDataSubscriber<CloseableReference<CloseableImage>> {
    private String mUri;
    private Target mTarget;
    private Options mOptions;
    private DisplayListener mListener;

    public BitmapDataSubscriber(String uri, Target target, Options options, DisplayListener listener) {
        mListener = listener;
        mOptions = options;
        mTarget = target;
        mUri = uri;
        onStart();
    }

    void onStart() {
        Options options = mOptions;
        if (options != null) {
            setTargetDrawable(options.placeHolderImage);
        }
        DisplayListener listener = mListener;
        if (listener instanceof OnStartListener) {
            ((OnStartListener) listener).onStart(mUri, getView());
        }
    }

    private void setTargetDrawable(Drawable drawable) {
        Target target = mTarget;
        if (target != null && drawable != null) {
            target.setDrawable(drawable);
        }
    }

    @Override
    public void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
        if (!dataSource.isFinished()) {
            return;
        }

        CloseableReference<CloseableImage> closeableImageRef = dataSource.getResult();
        Bitmap bitmap = null;
        CloseableImage closeableImage = null;
        if (closeableImageRef != null) {
            closeableImage = closeableImageRef.get();
            if (closeableImage instanceof CloseableBitmap) {
                bitmap = ((CloseableBitmap) closeableImage).getUnderlyingBitmap();
            }
        }

        try {
            // 你可以直接在这里使用Bitmap，没有别的限制要求，也不需要回收
            Target target = mTarget;
            if (target != null) {
                target.setBitmap(bitmap);
            }

            DisplayListener listener = mListener;
            if (listener != null) {
                listener.onSuccess(mUri, getView(), closeableImage);
            }

            if (target instanceof CloseableBitmapTarget && closeableImageRef != null) {
                // set closeableReference to target
                CloseableBitmapTarget closeableBitmapTarget = (CloseableBitmapTarget) target;
                closeableBitmapTarget.setCloseableReference(closeableImageRef);
            }
        } finally {
            CloseableReference.closeSafely(closeableImageRef);
        }
    }

    @Override
    protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
        Options options = mOptions;
        if (options != null) {
            setTargetDrawable(options.failureImage);
        }
        DisplayListener listener = mListener;
        if (listener != null) {
            listener.onFailure(mUri, getView(), dataSource.getFailureCause());
        }
    }

    View getView() {
        Target target = mTarget;
        return target != null ? target.getView() : null;
    }
}